﻿#coding:utf-8
from django.db import models
from django.contrib.auth.models import User

from datetime import *
# Create your models here.
'''
class AbstractType(models.Model):  
    parent = models.ForeignKey('self',blank=True, null=True, related_name='child')  
    class Meta:  
        abstract = True
'''        
class BookType(models.Model):
    CHECK_CHOICES=(('literrature','文学'),('children','少儿'),('arts','艺术'),
    ('economy','经济'),('biology','生物'),('computer','计算机'),
    ('drama','戏剧'),('classic','古典'),('language','语言艺术'),
    ('photography','摄影'),('architecture&design','建筑设计'),('fiction','少儿小说'),
    ('nonfiction','少儿非小说'),('economy&law','经济法'),('e-business','电子商务'),
    ('economy&math','经济数学'),('molecule','分子生物学'),('cells','细胞学'),('plants','植物学'),
    ('software','软件编程'),('hardware','硬件'),('games','游戏娱乐'),)
    booktype=models.CharField(max_length=50,choices=CHECK_CHOICES,unique=True)
    parenttype=models.ForeignKey('self',null=True,blank=True, default=None,related_name='child')
    
    def __unicode__(self):
        return self.booktype

#checked
class TopicType(models.Model):
    CHECK_CHOICES=(('topic3','主题3'),('topic2','主题2'),('topic1','主题1'),('none','无主题'))
    topic=models.CharField(max_length=50,choices=CHECK_CHOICES,primary_key=True)
    title=models.CharField(max_length=50)
    
    def __unicode__(self):
        return self.title
    

class Book(models.Model):

    booktype=models.ManyToManyField(BookType)
    bookname=models.CharField(max_length=50)
    topic=models.ForeignKey(TopicType,null=True,blank=True,default=None) 
    #
    market_price=models.DecimalField(max_digits=8,decimal_places=2)
    in_price=models.DecimalField(max_digits=8,decimal_places=2)
    quantity=models.IntegerField(default=0)
    #
    author=models.CharField(max_length=30)
    publisher=models.CharField(max_length=50)
    productdate=models.DateField()
    description=models.TextField()
    bigimage=models.ImageField(upload_to='upload')
    smallimage=models.ImageField(upload_to='upload')
    ISBN=models.CharField(max_length=50,unique=True)
    #
    lossquantity=models.IntegerField(default=0)
 
    def getlowprice(self):
        pass
    def getbestseller(self):
    
        pass    
    def __unicode__(self):
        return self.bookname   
class BookInfo():
    def __init__(self):  
        pass
    def get_lowbook(self,price):
        list=Book.objects.filter(in_price__lte=price)
        return list
    def get_bestseller(self,book):
        pass
class LineItem(models.Model):  
    book = models.ForeignKey(Book)  
    unit_price = models.DecimalField(max_digits=8,decimal_places=2)  
    quantity = models.IntegerField()  
class Cart():  
    def __init__(self):  
        self.items = []  
        self.total_price = 0  
    def add_product(self,book):  
        self.total_price += book.in_price  
        for item in self.items:  
            if item.book.id == book.id:  
                item.quantity += 1   
                return  
        l=LineItem(book=book,unit_price=book.in_price,quantity=1)
        
        self.items.append(l)
    def del_product(self,book):
        print "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
        for item in self.items: 
            if item.book.id==book.id:
                print "found it++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
                self.total_price -= item.unit_price*item.quantity
                self.items.remove(item)
                return
    def get_sumitems(self):
        num=0
        for item in self.items:
            num=num+item.quantity
        return num
class Address(models.Model):
    #默认配送信息
    CHECK_CHOICES=(('COD','货到付款'),('CARD','信用卡'),('Third','第三方'),)
    DELIVERY_CHOICES=(('post','邮递'),('home','送货上门'),)
    consumer=models.OneToOneField(User)
    recepit=models.CharField(max_length=50)
    tel=models.CharField(max_length=50)
    address = models.CharField(max_length=70, blank=True)
    check_method=models.CharField(max_length=70,choices=CHECK_CHOICES)
    delivery_method=models.CharField(max_length=70,choices=DELIVERY_CHOICES)
    
    def __unicode__(self):
        return self.user

#checked
class Order(models.Model):
    cusumer = models.ForeignKey(User)
    date_creation = models.DateField(blank=True, default=date.today())
    STATE_CHOICES=(('processing','处理中'),('processed','处理完毕'),('invalid','无效'),)
    state=models.CharField(max_length=30,choices=STATE_CHOICES)
    
    total_price = models.DecimalField(max_digits=12, decimal_places=2, blank=True, default=0)
    
    CHECK_CHOICES=(('COD','货到付款'),('CARD','信用卡'),('Third','第三方'),)
    DELIVERY_CHOICES=(('post','邮递'),('home','送货上门'),)
    check_method=models.CharField(max_length=70,choices=CHECK_CHOICES)
    delivery_method=models.CharField(max_length=70,choices=DELIVERY_CHOICES)
    date_delivery=models.DateField(blank=True,default=date.today())
    
    
    recepit=models.CharField(max_length=50)
    tel=models.CharField(max_length=50)
    address = models.CharField(max_length=70,blank=True,null=False)
    def __unicode__(self):
        return self.id
class OrderItem(models.Model):
    order = models.ForeignKey(Order)
    book = models.ForeignKey(Book)
    quantity = models.DecimalField(max_digits=12, decimal_places=2)
    unit_price = models.DecimalField(max_digits=12, decimal_places=2)
    total_price = models.DecimalField(max_digits=12, decimal_places=2, blank=True, default=0)
    def __unicode__(self):
        return self.id 

class BuyIn(models.Model):
    time=models.DateField(blank=True, default=date.today())
    book=models.OneToOneField(Book)
    num=models.IntegerField(default=0)
    sum=models.DecimalField(max_digits=12, decimal_places=2, blank=True, default=0)
    
class SaleOut(models.Model):
    time=models.DateField(blank=True, default=date.today())
    book=models.OneToOneField(Book)
    num=models.IntegerField(default=0)
    sum=models.DecimalField(max_digits=12, decimal_places=2, blank=True, default=0)
    pass